{/* Copyright 2025 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License. */}

import {Layout} from '../../src/Layout';
export default Layout;
import {FunctionAPI} from '../../src/FunctionAPI';
import docs from 'docs:@react-aria/interactions';
import typesDocs from 'docs:@react-types/shared/src/events.d.ts';
import {InterfaceType} from '../../src/types';
import {InlineAlert, Heading, Content} from '@react-spectrum/s2';

export const section = 'Interactions';
export const description = 'Handles long press interactions across mouse and touch devices.';

# useLongPress

<PageDescription>{docs.exports.useLongPress.description}</PageDescription>

```tsx render
"use client";
import React from 'react';
import {mergeProps} from 'react-aria';
import {useLongPress, usePress} from '@react-aria/interactions';

function Example() {
  let [events, setEvents] = React.useState([]);
  let [mode, setMode] = React.useState('Activate');

  /*- begin focus -*/
  // Long press to activate "Hyper speed"
  let {longPressProps} = useLongPress({
    accessibilityDescription: 'Long press to activate hyper speed',
    onLongPressStart: e => setEvents(
      events => [`long press start with ${e.pointerType}`, ...events]
    ),
    onLongPressEnd: e => setEvents(
      events => [`long press end with ${e.pointerType}`, ...events]
    ),
    onLongPress: e => {
      setMode('Hyper speed');
      setEvents(
        events => [`long press with ${e.pointerType}`, ...events]
      );
    }
  });
  /*- end focus -*/

  // Short press to activate "Normal speed"
  let {pressProps} = usePress({
    onPress: e => {
      setMode('Normal speed');
      setEvents(
        events => [`press with ${e.pointerType}`, ...events]
      );
    }
  });

  return (
    <>
      <button {...mergeProps(pressProps, longPressProps)}>{mode}</button>
      <ul
        style={{
          maxHeight: '200px',
          overflow: 'auto'
        }}>
        {events.map((e, i) => <li key={i}>{e}</li>)}
      </ul>
    </>
  );
}
```

<InlineAlert variant="notice">
  <Heading>Accessibility</Heading>
  <Content>This example does not have a keyboard accessible way to trigger the long press action. Because the method of triggering
this action will differ depending on the component, it is outside the scope of `useLongPress`. Make sure to implement a keyboard
friendly alternative to all long press interactions if you are using this hook directly.</Content>
</InlineAlert>

## Features

A long press is triggered when a user presses and holds their pointer over a target for a minimum period of time. If the user moves their pointer off of the target before the time threshold, the interaction is canceled. Once a long press event is triggered, other pointer interactions that may be active such as `usePress` and `useMove` will be canceled so that only the long press is activated.

* Handles mouse and touch events
* Prevents text selection on touch devices while long pressing
* Prevents browser and OS context menus from appearing while long pressing
* Customizable time threshold for long press
* Supports an accessibility description to indicate to assistive technology users that a long press action is available

## API

<FunctionAPI function={docs.exports.useLongPress} links={docs.links} />

### LongPressProps

<InterfaceType {...docs.exports.LongPressProps} />

### LongPressResult

<InterfaceType {...docs.exports.LongPressResult} />

### LongPressEvent

Each of these handlers is fired with a `LongPressEvent`, which exposes information about the target and the
type of event that triggered the interaction.

<InterfaceType {...typesDocs.exports.LongPressEvent} />